package com.treasure.mall.biz.entity.live;

import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.zbq.common.Jsr310Utils;
import lombok.Data;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 *
 * <p> 直播间用户
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.entity.live
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/16 6:01 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Entity
@Data
@Table(name = "zb_live_room_user")
@Cacheable(false)
public class LiveRoomUserPO extends BasePO {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 用户
     */
    @ManyToOne
    @JoinColumn(name = "rel_user_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private UserPO user;

    /**
     * 直播间sessionId
     */
    @Column(name = "session_id", length = 50)
    private String sessionId;

    /**
     * 首次进入时间
     */
    @Column(name = "last_enter_time")
    private LocalDateTime firstEnterTime;
    /**
     * 最后离开时间
     */
    @Column(name = "last_leave_time")
    private LocalDateTime lastLeaveTime;

    /**
     * 当前是否在线
     */
    @Column(name = "is_online")
    private boolean online = true;
    /**
     * 是否禁言
     */
    @Column(name = "is_forbid")
    private boolean forbid = false;
    /**
     * 观看次数
     */
    @Column(name = "enter_count")
    private long enterCount = 0;

    /***
     * 用户在线总时长，单位秒
     */
    @Column(name = "user_online_total_seconds")
    private long userOnlineTotalSeconds = 0;


    /**
     * 观看总时长
     *
     * @return
     */
    public String getTotalDuration() {
        return Jsr310Utils.Duration.getDurationBySeconds(userOnlineTotalSeconds);
    }

    /**
     * 平均时长
     *
     * @return
     */
    public String getAvgDuration() {
        if (userOnlineTotalSeconds == 0) {
            return Jsr310Utils.Duration.getDurationBySeconds(userOnlineTotalSeconds);
        }
        return Jsr310Utils.Duration.getDurationBySeconds(userOnlineTotalSeconds / enterCount);
    }
}
